package com.lsf.dunlian.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.adapter.NoticeRecyclerViewAdapter;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.databinding.ActivityAttendanceBinding;
import com.lsf.dunlian.databinding.ActivityNoticeBinding;
import com.lsf.dunlian.entity.AllNoticeRecordData;
import com.lsf.dunlian.entity.AllNoticeRecordEntity;
import com.lsf.dunlian.entity.ChatRecordEntity;
import com.lsf.dunlian.entity.ChatRecordVo;
import com.lsf.dunlian.entity.CurrentAttendanceVo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class NoticeActivity extends Activity {
    private LayoutInflater inflater;
    private ActivityNoticeBinding binding;
    private NoticeRecyclerViewAdapter adapter;
    private List<AllNoticeRecordData> dataList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseUtils.setStatusBar(this);
        inflater = LayoutInflater.from(this);
        binding = DataBindingUtil.inflate(inflater, R.layout.activity_notice, null, false);
        setContentView(binding.getRoot());
        adapter = new NoticeRecyclerViewAdapter(dataList,this);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);
        binding.fabu.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG );
        binding.fabu.setOnClickListener(v -> startActivity(new Intent(NoticeActivity.this,CreateNoticeActivity.class)));
        if ("f25bf33a-05f4-4777-bbe6-d5d1fba07bec".equals(PreferenceUtil.getSharedPreference(Constant.USERID))){
            binding.fabu.setVisibility(View.VISIBLE);
        }
    }

    private void get(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.NOTICE_LIST).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) { }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    AllNoticeRecordEntity vo = new Gson().fromJson(result, AllNoticeRecordEntity.class);
                    if (vo==null || vo.data==null) return;
                    dataList.clear();
                    dataList.addAll(vo.data);
                    runOnUiThread(() -> {
                        adapter.notifyDataSetChanged();
                    });
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        get();
    }
}
